﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TaskManager.Data;
using System.Web.Script.Serialization;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace TaskManager.Views
{
    public partial class SprintPage : System.Web.UI.Page
    {
        protected Sprint _sprint;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.RequestType != "POST")
            {
                using (IUnityContainer container = new UnityContainer())
                {
                    container.LoadConfiguration();
                    ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                    Sprint sprint = sprintRepository.GetSprints().Where(s => s.SprintId == Convert.ToInt32(Request.QueryString["id"])).First();
                    this.SprintId.Value = sprint.SprintId.ToString();
                    this.StartDate.Text = sprint.StartDate.ToString();
                    this.EndDate.Text = sprint.EndDate.ToString();
                }
            }
        }

        [System.Web.Services.WebMethod]
        public static object GetDevelopers()
        {
            IList<Developer> developers = null;

            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                developers = sprintRepository.GetDevelopers().ToList();
            }
            return developers.Select(d => new
            {
                DeveloperId = d.DeveloperId,
                Title = d.Title,
                FirstName = d.FirstName,
                LastName = d.LastName,
                Name = d.Name
            });
        }

        [System.Web.Services.WebMethod]
        public static object GetSprintTasks(int id)
        {
            IList<Task> tasks = null;

            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                Sprint sprint = sprintRepository.GetSprints().Where(s => s.SprintId == id).FirstOrDefault();
                tasks = sprint.GetTasks();
            }
            return tasks.Select(t => new
            {
                TaskId = t.TaskId,
                SprintId = t.SprintId,
                TypeOfTask = t.TypeOfTask,
                EstimatedHours = t.EstimatedHours,
                Description = t.Description,
                AssignedToId = t.AssignedToId,
                CompletedDate = t.CompletedDate
            });
        }

        [System.Web.Services.WebMethod]
        public static object UpdateSprint(string json)
        {
            string message = string.Empty;
            Sprint sprint = new JavaScriptSerializer().Deserialize(json, typeof(Sprint)) as Sprint;
            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                message = sprintRepository.SaveSprint(sprint);
            }
            return new { message = message };
        }

        [System.Web.Services.WebMethod]
        public static object SaveTasks(string models)
        {
            IList<Task> tasks = new JavaScriptSerializer().Deserialize(models, typeof(IList<Task>)) as IList<Task>;
            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                foreach (Task task in tasks)
                {
                    sprintRepository.SaveTask(task, false);
                }
            }
            return new { result = "complete" };
        }

        [System.Web.Services.WebMethod]
        public static object DeleteTasks(string models)
        {
            IList<Task> tasks = new JavaScriptSerializer().Deserialize(models, typeof(IList<Task>)) as IList<Task>;
            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                foreach (Task task in tasks)
                {
                    sprintRepository.SaveTask(task, true);
                }
            }
            return new { result = "complete" };
        }
    }
}